package com.sangcomz.fishbun.util;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.DataAbilityHelper;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.media.photokit.metadata.AVStorage;
import ohos.utils.PacMap;

import java.io.File;
import java.io.IOException;
import java.nio.file.OpenOption;
import java.text.SimpleDateFormat;
import java.util.Date;

public class CameraUtil {

    public String savedPath = null;

    public void takePicture(AbilitySlice abilitySlice, String saveDir, int requestCode) {
    }

    public void saveImage(DataAbilityHelper dataAbilityHelper, File file) {
        PacMap values = new PacMap();
        values.putString(AVStorage.Images.Media.DISPLAY_NAME, file.getName());
        values.putString(AVStorage.Images.Media.MIME_TYPE, "image/jpg");
        values.putIntValue(AVStorage.Audio.Media.OUTPUT, 1);

        //TODO 暂未移植
//        contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values)?.let {
//            contentResolver.openFileDescriptor(it, "w").use { parcelFileDescriptor ->
//                    val fileDescriptor = parcelFileDescriptor?.fileDescriptor ?: return@use
//
//                    FileOutputStream(fileDescriptor).use { fileOutputStream: FileOutputStream ->
//                    val inputStream = file.inputStream()
//                inputStream.use {
//                    fileOutputStream.write(it.readBytes())
//                }
//            }
//            }
//
//            values.clear()
//            values.put(MediaStore.Images.Media.IS_PENDING, 0)
//            contentResolver.update(it, values, null, null)
//        }
    }

    private File createImageFile(String saveDir) throws IOException {
        File dir = new File(saveDir);
        if (!dir.exists()) {
            dir.mkdirs();
        }

        //Create an image file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "JPEG_" + timeStamp + "_";
        File storageDir = new File(saveDir);

        return File.createTempFile(imageFileName, /* prefix */
                ".jpg", /* suffix */
                storageDir /* directory */
        );
    }

    public String getSavedPath() {
        return savedPath;
    }

    public void setSavedPath(String savedPath) {
        this.savedPath = savedPath;
    }
}
